package com.baosight.risk.service.rire.impl;

import com.baosight.risk.api.rire.RireRiskEventService;
import com.baosight.risk.api.xtzy.IXtzyIntegralService;
import com.baosight.risk.base.entity.MultipleUpdateStatus;
import com.baosight.risk.base.service.BaseServiceImpl;
import com.baosight.risk.common.bean.BeanUtils;
import com.baosight.risk.common.enums.IntegralType;
import com.baosight.risk.common.utils.DateUtils;
import com.baosight.risk.common.utils.StringUtils;
import com.baosight.risk.mapper.rire.RireRiskEventMapper;
import com.baosight.risk.mapper.xtzy.XtzyCompanyMapper;
import com.baosight.risk.service.rire.entity.RireRiskEvent;
import com.baosight.risk.service.rire.vo.RireRiskEventVO;
import com.baosight.risk.service.riri.entity.RiriRisk;
import com.baosight.risk.service.riri.impl.RiaaRiskServiceImpl;
import com.baosight.risk.service.xtzy.entity.XtzyCompany;
import com.baosight.risk.service.xtzy.entity.XtzyUser;
import com.baosight.risk.service.xtzy.impl.XtzyCompanyServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class RireRiskServiceImpl extends BaseServiceImpl<RireRiskEvent> implements RireRiskEventService {

    @Autowired
    private XtzyCompanyMapper companyMapper;
    @Autowired
    private RireRiskEventMapper eventMapper;
    @Autowired
    private RiaaRiskServiceImpl riaaRiskService;
    @Autowired
    private XtzyCompanyServiceImpl xtzyCompanyService;
    @Autowired
    private IXtzyIntegralService integralService;

    @Override
    public List<RireRiskEvent> selectList(RireRiskEventVO eventVO, XtzyUser user) {
        if (null != user) {
            eventVO.setCompIdList(riaaRiskService.getCompCodes(user.getExt1(), eventVO.getExt5()));
        }
        if (StringUtils.isNotEmpty(eventVO.getDeptCode())) {
            List<String> compList = new ArrayList<>();
            compList.add(eventVO.getDeptCode());
            //找本公司及一下公司
            XtzyCompany company = companyMapper.selectByPrimaryKey(eventVO.getDeptCode());
            if (Integer.valueOf(company.getPathLevel()) != 1) {
                //并非末级，找下级子公司
//                if (!"1".equals(company.getEndLevelFlag())) {
                if (1 != company.getEndLevelFlag()) {
                    List<XtzyCompany> companies = new ArrayList<>();
                    xtzyCompanyService.getNextCompany(company.getId(), companies);
                    companies.add(company);
                    compList.addAll(companies.stream().map(XtzyCompany::getId).collect(Collectors.toList()));
                }
            }
            eventVO.setCompIdList(compList);
        }
        servicePage();
        return eventMapper.selectList(eventVO);
    }

    @Override
    public List<RireRiskEvent> checkEventList(RireRiskEvent rireRiskEvent) {
        return eventMapper.checkEventList(rireRiskEvent);
    }

    @Override
    public Boolean deleteByIds(MultipleUpdateStatus updateStatus) {
        for (String id : updateStatus.getIds()) {
            RireRiskEvent event = eventMapper.selectByPrimaryKey(id);
            if (StringUtils.isNotEmpty(event.getImportGuid())) {
                return false;
            }
        }
        eventMapper.deleteByIds(updateStatus);
        return true;
    }

    @Override
    public Integer saveRireEvent(List<RireRiskEvent> rireRiskEventList, XtzyUser user) throws Exception {
        for (RireRiskEvent rireRiskEvent : rireRiskEventList) {
            log.info("保存风险库参数：" + rireRiskEvent.toString());
            if (StringUtils.isEmpty(rireRiskEvent.getId())) {
                //获取当前年度
                rireRiskEvent.setAcctYear(DateUtils.format(new Date(), "yyyy"));
                //获取当年季度
                rireRiskEvent.setQuarterCode(DateUtils.getQuarter(new Date()));
                rireRiskEvent.setQuarterName(DateUtils.getQuarterName(rireRiskEvent.getQuarterCode()));
                //数据校验
                List<RireRiskEvent> list = eventMapper.checkEventList(rireRiskEvent);
                if (CollectionUtils.isNotEmpty(list)) {
                    log.error(rireRiskEvent.getDeptName() + "=该公司下已经存在风险名称为【" + rireRiskEvent.getRiskName() + "】事件名称为【" + rireRiskEvent.getEventName() + "】的数据！");
                    throw new Exception(rireRiskEvent.getDeptName() + "=该公司下已经存在风险名称为【" + rireRiskEvent.getRiskName() + "】事件名称为【" + rireRiskEvent.getEventName() + "】的数据！");
                }
                rireRiskEvent = (RireRiskEvent) BeanUtils.initBean(rireRiskEvent);
                insertEvent(rireRiskEvent, user);
            } else {
                rireRiskEvent = (RireRiskEvent) BeanUtils.updateBean(rireRiskEvent);
                updateRireEvent(rireRiskEvent);
            }
        }
        return 1;
    }

    public void insertEvent(RireRiskEvent rireRiskEvent, XtzyUser user) {
        XtzyCompany company = companyMapper.getCompanyInfo(rireRiskEvent.getDeptCode());
        //如果公司类型是1或者是1，那么公司和部门都是同样的
//        if ("2".equals(company.getPathLevel()) || "1".equals(company.getPathLevel())) {
        if (2 == company.getPathLevel() || 1 == company.getPathLevel()) {
            rireRiskEvent.setCompCode(rireRiskEvent.getCompCode());
            rireRiskEvent.setCompName(rireRiskEvent.getCompName());
        } else {
            //获取一级子公司或部门
            company = companyMapper.getTopCompany(company.getPathCode(), Short.parseShort("2"));
            rireRiskEvent.setCompCode(company.getCompCode());
            rireRiskEvent.setCompName(company.getCompName());
        }
        eventMapper.insert(rireRiskEvent);
        //添加积分
        integralService.insertIntegral(user, IntegralType.Type9);
    }

    @Override
    public Integer updateRireEvent(RireRiskEvent rireRiskEvent) {
        return eventMapper.updateByPrimaryKeySelective(rireRiskEvent);
    }

    @Override
    public List<RireRiskEvent> getEventList(RireRiskEventVO eventVO, XtzyUser user) {

        String[] strs = {"RISK01", "RISK02", "COMP01", "COMP05", "SUPERROLE", "COMP03", "RISK03"};
        String ext5 = user.getExt5();
        String[] split = ext5.split("\\[|,|\\]");
        //对比权限方法
        boolean b = loopStr(strs, split);
        if (b) {
            eventVO.setCompIdList(new ArrayList<>());
            return eventMapper.selectList(eventVO);
        }
        List<String> compList = new ArrayList<>();
        compList.add(user.getExt1());
        //找本公司及一下公司
        XtzyCompany company = companyMapper.selectByPrimaryKey(user.getExt1());
        if (Integer.valueOf(company.getPathLevel()) != 1) {
            //并非末级，找下级子公司
            if (1 != company.getEndLevelFlag()) {
                List<XtzyCompany> companies = new ArrayList<>();
                xtzyCompanyService.getNextCompany(company.getId(), companies);
                companies.add(company);
                compList.addAll(companies.stream().map(XtzyCompany::getId).collect(Collectors.toList()));
            }

        }
        eventVO.setCompIdList(compList);
        return eventMapper.selectList(eventVO);
    }

    @Override
    public List<RireRiskEvent> queryRiskEventData(RireRiskEventVO rireRiskEventVO) {
        List<RireRiskEvent> riskEventList =eventMapper.queryRiskEventData(rireRiskEventVO);
        return  riskEventList;
    }

    private boolean loopStr(String[] s1, String[] s2) {
        boolean flag = false;
        for (String s : s1) {
            for (String s3 : s2) {
                if (s3.trim().equals(s.trim())) {
                    flag = true;
                    break;
                }
            }
        }
        return flag;
    }
}
